'use strict';
const Controller = require('egg').Controller;
const svgCaptcha = require("svg-captcha");
class LoginController extends Controller {
  async login(){
      const {ctx} = this
      let req = ctx.request.body
      let data = await ctx.service.login.login(req)
      if(data!=undefined &&data.pwd == req.pwd){
        ctx.body=true
        // console.log(data)
        ctx.cookies.set("userid",data.id)//设置后端缓存用户的id
      }else{
        ctx.body = false
      }
  }
  async verfy() {
    let verifyObj = svgCaptcha.create({
        size: 4,
        fontSize: 50,
        width: 150,
        height: 40,
        background: "#f3fbfe",
        // charPreset:"abdc" 
    })
    // console.log(verifyObj.text)
    this.ctx.body = verifyObj.data
    this.ctx.cookies.set("svg",verifyObj.text)
    // console.log(verifyObj.text)
}
  async register(){
    const {ctx} = this
    let obj = ctx.request.body
    // console.log(obj.vcode.toLocaleUpperCase())
    if(obj.vcode.toLocaleUpperCase()== ctx.cookies.get("svg").toLocaleUpperCase()){
      let data = await ctx.service.login.register(obj)
      ctx.body = data
    }else{
      ctx.body = {code:4001} //验证码错误返回code码4001
    }
    // console.log(obj)
  }
}
  
module.exports = LoginController;